package cn.imvc.app.controllers;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.imvc.app.commons.SystemConfig;
import cn.imvc.app.entities.Comments;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.CommentsModel;
import cn.imvc.app.models.GoodsOrderModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.app.models.SettingModel;
import cn.imvc.core.Render;

@Controller
@RequestMapping("/comments")
public class CommentsControllerImpl implements CommentsController {
	@Autowired
	private CommentsModel commentsModel;

	@Autowired
	MemberModel memberModel;
	
	@Autowired
	SettingModel settingModel;

	@Autowired
	GoodsOrderModel gooodsOrderModel;

	@Autowired
	SystemConfig systemConfig;

	/**
	 * 获取商品评论分页数据
	 * 
	 * @param goodsId
	 * @param page
	 * @param sort
	 *            格式 : col1_orderType1,col2_orderType2,...
	 * @return
	 */
	@RequestMapping("/list.html")
	public String getPagedComments(Integer goodsId, Integer page, String sort,Model model) {
		if(sort==null||"".equals(sort)){
			sort = "addtime_desc";
		}
		Page<Comments>  result = commentsModel.findByGoodsId(goodsId, page,CommentsModel.PAGE_SIZE, sort);
		model.addAttribute("pagedComments", result);
		model.addAttribute("goodsId", goodsId);
		return "comments/list";
	}

	@RequestMapping("/nextPageComments.json")
	public @ResponseBody Page<Comments> nextPageComments(Integer goodsId, Integer page, String sort) {
		if(sort==null||"".equals(sort)){
			sort = "addtime_desc";
		}
		return commentsModel.findByGoodsId(goodsId, page,CommentsModel.PAGE_SIZE, sort);
	}
	
	@RequestMapping("/comment.html")
	public String comment(String token, Model model, Integer goodsOrderId){
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("goodsOrderId", goodsOrderId);
			model.addAttribute("goodsOrder", gooodsOrderModel.get(goodsOrderId));
			return "comments/comment";
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/upload.html")
	@ResponseBody
	public String upload(Model model, String token, @RequestParam MultipartFile[] images) {
		Map<String, String> map = new HashMap<String, String>();
		Member member = memberModel.getLoginMember(token);
		if (member == null) {
			map.put("code", "-1");
		} else {
			String path = systemConfig.getCommentsImageFolder();
			map = commentsModel.upload(images, path);
		}
		return Render.json(map);
	}

	@RequestMapping("/upload.htm")
	@ResponseBody
	public String pcupload(Model model, String token, @RequestParam MultipartFile file) {
		Map<String, String> map = new HashMap<String, String>();
		Member member = memberModel.getLoginMember(token);
		if (member == null) {
			map.put("code", "-1");
		} else {
			String path = systemConfig.getCommentsImageFolder();
			Map<String, String> result = settingModel.upload(file, path);
			if("1".equals(result.get("code"))){
				map.put("image", result.get("fileUrl"));
				map.put("code", "1");
			}
		}
		return Render.json(map);
	}
	
	@RequestMapping("/addComment.html")
	@ResponseBody
	public String addComment(String token, Comments comment, Integer goodsOrderId) {
		Member member = memberModel.getLoginMember(token);
		Map<String, String> result = commentsModel.addComment(member, comment, goodsOrderId);
		return Render.json(result);
	}
	
	@RequestMapping("/comment.htm")
	public String pccomment(String token, Model model, Integer goodsOrderId){
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("goodsOrderId", goodsOrderId);
			model.addAttribute("goodsOrder", gooodsOrderModel.get(goodsOrderId));
			return "comments/pccomment";
		} else {
			return "redirect:/member/login.htm";
		}
	}
	/**
	 * 获取商品评论分页数据
	 * 
	 * @param goodsId
	 * @param page
	 * @param sort
	 *            格式 : col1_orderType1,col2_orderType2,...
	 * @return
	 */
	@RequestMapping("/list.htm")
	public String pcGetPagedComments(Integer goodsId, Integer page, String sort,Model model) {
		if(sort==null||"".equals(sort)){
			sort = "addtime_desc";
		}
		if(page!=null&&page>0){
			page--;
		}
		Page<Comments>  result = commentsModel.findByGoodsId(goodsId, page,CommentsModel.GOODS_PAGE_SIZE, sort);
		model.addAttribute("pagedComments", result);
		model.addAttribute("goodsId", goodsId);
		return "comments/pcpage_content";
	}
}
